import { RoleListParam } from "@/api/role/roleModel";
import { onMounted, reactive } from "vue";
import { getUserId } from "@/utils/auth";
import { getRoleListApi } from "@/api/role/role";

export default function tableList() {
    // 定义表格数据
    const tableData = reactive({
        list: []
    })
    // 定义列表查询的参数
    const listQuery = reactive<RoleListParam>({
        userId: getUserId() || '',
        currentPage: 1,
        pageSize: 10,
        name: '',
        total: 0
    })
    // 获取角色列表
    const getRoleList = async () => {
        let res = await getRoleListApi(listQuery)
        if(res && res.code === 200) {
            // console.log(res)
            tableData.list = res.data.records
            listQuery.total = res.data.total
        }
    }
    // 改变页容量
    const sizeChange =  (size:number) => {
        listQuery.pageSize = size
        getRoleList()
    }

    // 页数改变
    const currentChange = async (page:number) => {
        listQuery.currentPage = page
        getRoleList()
    }
    // 重置
    const resetBtn = async () => {
        listQuery.name = ''
        getRoleList()
    }
    onMounted(() => {
        getRoleList()
    })
    return {
        tableData,
        listQuery,
        getRoleList,
        sizeChange,
        currentChange,
        resetBtn
    }
}